#include <wiringPi.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include "lvx_file.h"
#include "time_info.h"
#include "gprmc.h"

#define TOUCH_OUT_PIN 27
#define TOUCH_IN_PIN 26

int flag_exit_pps = 0;

#define USE_ISR_PIN 1

volatile int rising_flag = 0;
volatile int rising_time_refreshed = 0;
//bit0: gps
//bit1: lpms
//bit2: livox
//bit3: pps

extern void BCC(u8 *data,u8 *res);

volatile int g_rising_tv = 0;


void savePackage_pps(char * str_in)
{
    char * buff_write = (char *)malloc(128); 
    memset(buff_write,0,128);
    int len_str = strlen(str_in);    
    char * ptr_data = buff_write;    
    unsigned char sum[4];		
    printf("pps save:%s\r\n",str_in);    
    buff_write[0] = 0x7b;    
    buff_write[1] = 0x04;    
    buff_write[2] = len_str + 3;    
    buff_write[3] = 0x00;    
    buff_write[4] = 0x00;    
    buff_write[5] = 0x00;    
    memcpy(buff_write+6,str_in,len_str);       
    BCC((unsigned char *)str_in,sum);    
    buff_write[6+len_str] = '*';    
    buff_write[7+len_str] = sum[0];    
    buff_write[8+len_str] = sum[1];    
    ptr_data += (len_str + 9);        
    *ptr_data = 0x0d;    
    ptr_data++;    
    *ptr_data = 0x0a;    
    pthread_mutex_lock(&mutex_file_wr);    
    write_to_lvx((unsigned char *)buff_write, len_str+11,1);    
    pthread_mutex_unlock(&mutex_file_wr);
    //rising_flag = 0;
    free(buff_write);
    
}


void myInterrupt0 (void) 
{ 
    rising_flag = 1; 
}

char str_rise_time[64];
void main_pps(void)
{
    char buff[128];
    char buff_time[64];
    if(wiringPiSetup()==-1)
    {
        printf("pps , wiring setup failed\n");
        return;
     }
    pinMode(TOUCH_IN_PIN,INPUT);
    pullUpDnControl(TOUCH_IN_PIN,PUD_UP);

    //pinMode(TOUCH_OUT_PIN,OUTPUT);
    //digitalWrite(TOUCH_OUT_PIN,HIGH);
    g_rising_tv = 0;
    flag_exit_pps = 0;
    volatile int currState = 0;
    volatile int lastState = 0;
	
	g_run_duration->get_delta_str(str_rise_time); 

    struct timeval curr_tv;
    struct tm date_time_now;

    wiringPiISR (TOUCH_IN_PIN, INT_EDGE_RISING, &myInterrupt0) ;

   // currState = digitalRead(TOUCH_IN_PIN);

    //printf("currState is:%d\r\n",currState);

    while(!flag_exit_pps)
    {
#if (!USE_ISR_PIN)
        currState = digitalRead(TOUCH_IN_PIN);
        if(lastState != currState)
        {
             
             if(!lastState)
             {//low to high
                 printf("pps rising edge\r\n");
                 gettimeofday(&curr_tv, NULL);                 
                 localtime_r(&curr_tv.tv_sec,&date_time_now);                  
                 
                 buff[0] = '$';
                 strftime(buff_time,64,"%Y%m%d%H%M%S",&date_time_now);
                 sprintf(buff,"$%s.%03ld,",buff_time,curr_tv.tv_usec/1000);
                 //printf("start is %s\r\n",buff);

                  
             }
             else
             {//high to low
                printf("pps falling edge\r\n");
                gettimeofday(&curr_tv, NULL);                
                localtime_r(&curr_tv.tv_sec,&date_time_now);                  
                strftime(buff_time,64,"%Y%m%d%H%M%S",&date_time_now);
                sprintf(buff,"%s%s.%03ld",buff,buff_time,curr_tv.tv_usec/1000);
                savePackage(buff);
             }
        }
        lastState = currState;
#else
        if(rising_flag)
        {
            printf("pps rising edge\r\n");
            rising_flag = 0;
            rising_time_refreshed = 0x0f;
            g_run_duration->get_delta_str(str_rise_time);
            gettimeofday(&curr_tv, NULL);

            g_rising_tv = curr_tv.tv_sec;
        }

        if(rising_time_refreshed & 0x07)
        {
             gettimeofday(&curr_tv, NULL);
             if( (curr_tv.tv_sec>g_rising_tv) 
             && ((curr_tv.tv_sec-g_rising_tv) >= 2))
             {
                 rising_time_refreshed = rising_time_refreshed & 0x08;//set bit0, bit1, and bit2 to 0
             }
        }

 //       if(flag_gps)
 //       {
 //           sprintf(buff,"%s,%s",str_gps_utc,str_rise_time);
 //           flag_gps = 0;
 //       }
#endif
        //usleep(10);
        //sleep(1);
        //if(currState==0)
        //printf("currState:%d\n",currState);
    }
      return;
}


int main_test1(void)
{
    char buff[128];
    char buff_time[64];

    struct timeval curr_tv;
    struct tm date_time_now;

    gettimeofday(&curr_tv, NULL);
      time_t time_now = curr_tv.tv_sec;

      localtime_r(&curr_tv.tv_sec,&date_time_now);                  

      buff[0] = '$';
      strftime(buff_time,64,"%Y%m%d%H%M%S",&date_time_now);
      sprintf(buff,"$%s.%03ld,",buff_time,curr_tv.tv_usec/1000);
      printf("aa is %s\r\n",buff);
    sleep(1);

        gettimeofday(&curr_tv, NULL);
        time_now = curr_tv.tv_sec;
        
        localtime_r(&curr_tv.tv_sec,&date_time_now);                  
        strftime(buff_time,64,"%Y%m%d%H%M%S",&date_time_now);
        sprintf(buff,"%s%s.%03ld",buff,buff_time,curr_tv.tv_usec/1000);
    printf("%s\r\n",buff);
      
    return 0;
}
